﻿
Imports log4net.Core
Imports log4net.Appender
Imports log4net
Imports log4net.Repository.Hierarchy

Namespace log4net.Appender

    Public Class VirtualConsoleAppender
        Inherits Global.log4net.Appender.AppenderSkeleton

        Public Property Console As TL.Console.VirtualConsole

        Protected Overloads Overrides Sub Append(ByVal loggingEvent As LoggingEvent)
            If Console IsNot Nothing Then
                Console.WriteLine(RenderLoggingEvent(loggingEvent))
            End If
        End Sub

        Public Shared Sub SetConsole(ByVal tb As TL.Console.VirtualConsole)
            For Each appender As IAppender In GetAppenders()
                If TypeOf appender Is VirtualConsoleAppender Then
                    DirectCast(appender, VirtualConsoleAppender).Console = tb
                End If
            Next
        End Sub

        Private Shared Function GetAppenders() As IAppender()
            Dim appenders As New ArrayList()

            appenders.AddRange(LogManager.GetRepository().GetAppenders())
            For Each log As ILog In LogManager.GetCurrentLoggers()
                appenders.AddRange(DirectCast(log.Logger, Logger).Appenders)
            Next

            Return DirectCast(appenders.ToArray(GetType(IAppender)), IAppender())
        End Function

    End Class

End Namespace